//
//  YZAutoItemNormalVC.m
//  VET
//
//  Created by Lam BG on 2018/7/13.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZAutoItemNormalVC.h"

// auto model
#import "YZAutoVCModel.h"
#import "YZAutoHFModel.h"
#import "YZAutoRowModel.h"
#import "YZAutoSectionModel.h"

#import "YZBaseAutoCell.h"

// other
#import "YZAutoCache.h"
#import "YZAutoFormHeader.h"
#import "YZJsonFileLoader.h"
#import "YZAutoCellFactory.h"

// 第三方
#import "MJExtension.h"

// category
#import "NSArray+YZCategory.h"

@interface YZAutoItemNormalVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation YZAutoItemNormalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // 注册 cells
    [[YZAutoCellFactory sharedInstance] registerAutoItemCells:self.tableView];
}

- (void)loadData {
    [super loadData];
    
    // 填充数据
    NSString *action = self.avcModel.vcAction;
    NSAssert(action.length > 0, @"数据出错 请检查");

    [[[YZAutoCache sharedInstance] getAutoCellItems:action] subscribeNext:^(NSArray *items) {

        // 根据返回的数据生成界面
        [[YZAutoCellFactory sharedInstance] sections:self.data WithItems:items andVCModel:self.avcModel];
        [self.tableView reloadData];

    } error:^(NSError * _Nullable error) {

    }];
}

- (NSArray *)viewJson:(NSString *)fileName {
    
    NSDictionary *data = [YZJsonFileLoader loadLocalJsonWithFileName:fileName];
    if (data.count == 0) {
        
        data = [YZJsonFileLoader loadLocalJsonWithFileName:@"default"];
    }
    
    NSArray *jsonArray = data[@"sections"];
    
    return jsonArray;
}

#pragma mark - 子类实现

// rows in section nums
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    YZAutoSectionModel *sectionModel = self.data[section];
    NSArray *rows = sectionModel.itemRows;
    
    return rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 根据 row 类型生成 cell
    YZAutoRowModel *rowModel = (YZAutoRowModel*)[self.data itemRowInSection:indexPath];
    rowModel.indexPath = indexPath;
    
    YZAutoItemCell *cell = (YZAutoItemCell*)[[YZAutoCellFactory sharedInstance] generateCell:tableView withRowModel:rowModel];
    // 选中判断
    if (self.rowModel) rowModel.isSelect = [self.rowModel.paramValue isEqualToString:rowModel.paramValue];
    cell.rowModel = rowModel;
    
    @weakify(self)
    cell.selectItemBlock = ^(NSIndexPath *indexPath) {
        
        @strongify(self)
        YZAutoSectionModel *sectionModel = self.data[indexPath.section];
        NSArray *rows = sectionModel.itemRows;
        
        // 遍历之后把设置选中状态
        for (int i = 0; i < rows.count; i++) {
            
            YZAutoRowModel *rowModel = rows[i];
            BOOL isSelected = indexPath.row == i;
            rowModel.isSelect = isSelected;
            if (isSelected) {
                
                self.rowModel.paramValue   = rowModel.paramValue;
                self.rowModel.displayValue = rowModel.displayValue;
                
                if (self.itemClick) {
                    
                    self.itemClick(rowModel);
                }
            }
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    };
    
    return cell;
}

@end








